<?php
/**
 * Created by PhpStorm.
 * User: Llj_life
 * Date: 2018/1/29
 * Time: 23:04
 */

namespace app\admin\db;


class MysqliHelp extends \mysqli
{
    /**
     * MysqliHelp constructor.
     */

    public function __construct()
    {
        //面向对象风格
        //连接数据库
        $this->link=new \mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);

        //$this->link= parent::__construct(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);


        if ($this->link->connect_error) {
            die("连接失败" . $this->link->connect_error);
        }
        //$this->link->query("set names utf8");

    }

    //搜索语句
    function search_sql($sql)
    {
        //返回一个带数据的对象
        $result = $this->link->query($sql);
        //判断是否查询到数据
        $num=$result->num_rows;
        if($num==0){
            //查询不到数据
            //创建一个空对象返回
            $none = json_decode('');
            return $none;

        }else{
            //查询到数据
            return $result;
        }

    }

    //数据入库之前进行转义，确保数据能正确入库
    function escape($data){
        if(is_string($data)){
            return $this->link->real_escape_string($data);
        }
        if(is_array($data)){
            foreach($data as $key => $val){
                $data[$key]=$this->escape($val);
            }
        }
        return $data;
    }
    //关闭数据库连接
    function close()
    {
        parent::close(); // TODO: Change the autogenerated stub
    }
}
?>